//~--- JDK imports ------------------------------------------------------------

import java.io.*;
import java.util.Enumeration;
import java.util.Vector;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
 */
import javax.microedition.lcdui.*;

/**
 * @author Owner
 */
public class CanvasForm extends Canvas {
    private int    px               = getWidth() / 2;
    private int    py               = getHeight() / 2;
    private Vector listacoordenadas = new Vector();

    /**
     * constructor
     */
    protected CanvasForm() throws Exception {
        this.setFullScreenMode(false);

        InputStream  is = getClass().getResourceAsStream("obstaculos.txt");
        StringBuffer sb = new StringBuffer();

//      try
//      {
        int chars,
            i = 0;

        while ((chars = is.read()) != -1) {
            sb.append((char) chars);
        }

        System.out.print(sb.toString());
        
        Vector      coordenadas = split(sb.toString(), ",");
        Coordenadas coordenada  = new Coordenadas();

        System.out.print(coordenadas);
        System.out.print(coordenadas.elementAt(0));
        System.out.print(coordenadas.elementAt(0).getClass().getName());
        coordenada.x1 = Integer.parseInt(coordenadas.elementAt(0).toString()) ;
        coordenada.y1 = Integer.parseInt(coordenadas.elementAt(1).toString()) ;
        coordenada.x2 = Integer.parseInt(coordenadas.elementAt(2).toString()) ;
        coordenada.y2 = Integer.parseInt(coordenadas.elementAt(3).toString()) ;
        
        System.err.print(coordenada);
        System.err.print(listacoordenadas);
        listacoordenadas.addElement(coordenada);
        System.err.print(listacoordenadas);
/*
        }
        catch (Exception e)
        {
            throw e;
        } */
    }

    private Vector split(String cadena, String separador) {
        Vector items = new Vector();
        int    pos   = 0;

        pos = cadena.indexOf(separador);

        while (pos >= 0) {
            items.addElement(cadena.substring(0, pos).trim());
            cadena = cadena.substring(pos + separador.length());
            pos    = cadena.indexOf(separador);
        }

        items.addElement(cadena);

        return items;
    }

    protected void paint(Graphics g) {
        int width  = getWidth();
        int height = getHeight();
        System.out.print(listacoordenadas);
        for (Enumeration e = listacoordenadas.elements(); e.hasMoreElements(); ) {
            Coordenadas c = (Coordenadas) e.nextElement();

            g.setColor(255, 255, 255);
           g.fillRect(c.x1, c.y1, c.x2, c.y2);
          g.fillRect(10, 15, 50, 15);
      }

        g.setColor(0, 0, 0);
        g.fillRect(0, 0, width, height);
        g.setColor(255, 128, 0);
        g.drawRect(5, 5, width - 10, height - 10);
        g.setColor(255, 0, 0);
        g.fillRect(px, py, 10, 10);
    }

    /**
     * Called when a key is pressed.
     */
    protected void keyPressed(int keyC) {
        super.keyPressed(keyC);

        switch (keyC) {
        case Canvas.KEY_NUM8 :
            if (py < getHeight() - 15) {
                py = py + 3;
            }

            break;

        case Canvas.KEY_NUM2 :
            if (py > 10) {
                py = py - 3;
            }

            break;

        case Canvas.KEY_NUM6 :
            if (px < getWidth() - 15) {
                px = px + 3;
            }

            break;

        case Canvas.KEY_NUM4 :
            if (px > 10) {
                px = px - 3;
            }

            break;
        }

        this.repaint();
    }

    /**
     * Called when a key is released.
     */
    protected void keyReleased(int keyC) {}

    /**
     * Called when a key is repeated (held down).
     */
    protected void keyRepeated(int keyC) {}

    /**
     * Called when the pointer is dragged.
     */
    protected void pointerDragged(int x, int y) {}

    /**
     * Called when the pointer is pressed.
     */
    protected void pointerPressed(int x, int y) {}

    /**
     * Called when the pointer is released.
     */
    protected void pointerReleased(int x, int y) {}

    /**
     * Called when action should be handled
     */
    public void commandAction(Command c, Displayable d) {}
}



